package com.search.examples;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * A simple example, used on the jsoup website.
 */
@Slf4j
public class DoubanList {

    private static int bookSum = 0;

    private static String url = "https://www.douban.com/doulist/2753104";

    public static void main(String[] args) throws IOException {


        Document doc=null;
        boolean isEmpty = false;
        do{

            int currentPageBookSum = 0;
            try{
                doc = Jsoup.connect(url+"/?start="+bookSum).get();
            }catch (Exception e){
                log.error("请求豆瓣分页出错"+e.toString());
            }
            if(bookSum==0){
                log.info(doc.title());
                log.info(url);
            }
            log.debug("处理到第"+bookSum+"本书");
            Elements newsHeadlines = doc.getElementsByClass("doulist-item");
            currentPageBookSum = newsHeadlines.size();
            if(currentPageBookSum==0){
                isEmpty = true;
            }
            bookSum+=currentPageBookSum;
            for (Element headline : newsHeadlines) {
                Elements elements = headline.select("div.title");
                //评分
                Elements ratings = headline.getElementsByClass("rating_nums");
                float rating = Float.parseFloat(ratings.get(0).text().trim());
                String name =  elements.get(0).child(0).text();
                String url = elements.get(0).child(0).attr("href");

                //评分控制
                if(rating>8.0){
                    Document detail = null;
                    try{
                        detail = Jsoup.connect(url).get();
                    }catch (Exception e){
                        log.error("请求书籍详情出错"+e.toString());
                        continue;
                    }
                    //Elements infos = detail.select("div#info");
                    Element info = detail.getElementById("info");
                    int index=0;
                    for(Node node:info.childNodes()){
                        String text = node.toString();
                        if(text.contains("ISBN")){
                            //System.out.println(text);
                            //System.out.println(node.siblingIndex());
                            index = node.siblingIndex();
                        }
                    }
                    if(index>0){
                        //System.out.println(info.childNodes().get(index+1).toString());
                        String isbn = info.childNodes().get(index+1).toString();
                        //System.out.println(name+"------"+url+"------"+isbn);
                        TuTuSearch capitalSearch = new TuTuSearch();
                        if(capitalSearch.search(isbn.trim())){
                            log.info(name+"------"+url+"------"+isbn);
                        }
                    }
                }

            }

        }while (!isEmpty);
        log.info("\n\n\n\n\n\n");
    }

    private static void log(String msg, String... vals) {
        System.out.println(String.format(msg, vals));
    }
}
